
<template>
  <div style="margin-left: -5px">
    <!-- <div class="mask"
         v-show="isShowSelect"
         @click="isShowSelect = !isShowSelect"></div> -->
    <el-popover 
      placement="bottom-start"
      class="select-popover"
      width=190
      trigger="click"
      @hide="popoverHide"
      clearable>
      <el-tree class="common-tree tree-line"
               clearable
               ref="tree"
               :data="data"
               :props="defaultProps"
               :show-checkbox="multiple"
               :node-key="nodeKey"
               :check-strictly="checkStrictly"
               default-expand-all
               :expand-on-click-node="false"
               :default-checked-keys="defaultCheckedKeys"
               :highlight-current="true"
               @node-click="handleNodeClick"
               @check-change="handleCheckChange"></el-tree>
      <el-select 
                 slot="reference"
                 ref="select"
                 v-model="selectedData"
                 :multiple="multiple"
                 collapse-tags
                 clearable
                 @click.native="isShowSelect = !isShowSelect"
                 class="tree-select">
        <el-option v-for="item in options"
                   :key="item.value"
                   :label="item.label"
                   :value="item.value"></el-option>
      </el-select>
    </el-popover>
  </div>
</template>
 
<script>
export default {
  name: "tree-select",
  // props: ["clear"],
  props: {
    el: {
      type: String,
      default () {
        return ''
      }
    },
    // 树结构数据
    data: {
      type: Array,
      default () {
        return [];
      }
    },
    // 是否可选根节点
    rootNodeChick: Boolean,
    // 是否清空数据
    clear: Number,
    defaultProps: {
      type: Object,
      default () {
        return {};
      }
    },
    // 配置是否可多选
    multiple: {
      type: Boolean,
      default () {
        return false;
      }
    },
    nodeKey: {
      type: String,
      default () {
        return "id";
      }
    },
    // 显示复选框情况下，是否严格遵循父子不互相关联
    checkStrictly: {
      type: Boolean,
      default () {
        return false;
      }
    },
    // 默认选中的节点key数组
    checkedKeys: {
      type: Array,
      default () {
        return [];
      }
    },
    width: {
      type: Number,
      default () {
        return 260;
      }
    },
    height: {
      type: Number,
      default () {
        return 360;
      }
    },
  },
  data () {
    return {
      defaultCheckedKeys: [],
      isShowSelect: false, // 是否显示树状选择器
      options: [],
      selectedData: [], // 选中的节点
      // style: "width:" + this.width + "px;" + "height:" + this.height + "px;",
      // selectStyle: "width:" + (this.width + 24) + "px;",
      checkedIds: [],
      checkedData: []
    };
  },
  watch: {
    isShowSelect (val) {
      // 隐藏select自带的下拉框
      this.$refs.select.blur();
    },
    clear: function (n, o) {
      //箭头函数  不然会发生this改变
      if (n != 0) {
        this.selectedData = [];
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([]);
        });
      }
    },
    selectedData (newData, oldData) {
      this.popoverHide();
      if ( newData == undefined || newData == null || newData == [] || newData.length == 0 ) {
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([]);
        })
      }   
    }
  },
  mounted () {
    if (this.checkedKeys.length > 0) {
      if (this.multiple) {
        this.defaultCheckedKeys = this.checkedKeys;
        this.selectedData = this.checkedKeys.map(item => {
          var node = this.$refs.tree.getNode(item);
          return node.label;
        });
      } else {
        var item = this.checkedKeys[0];
        this.$refs.tree.setCurrentKey(item);
        var node = this.$refs.tree.getNode(item);
        this.selectedData = node.label;
      }
    }
  },
  methods: {
    loadCheckedKeys () {
      if (this.checkedKeys.length > 0) {
        if (this.multiple) {
          this.defaultCheckedKeys = this.checkedKeys;
          this.selectedData = this.checkedKeys.map(item => {
            var node = this.$refs.tree.getNode(item);
            return node.label;
          });
        } else {
          var item = this.checkedKeys[0];
          this.$refs.tree.setCurrentKey(item);
          var node = this.$refs.tree.getNode(item);
          this.selectedData = node.label;
        }
      }
    },
    changeIsShowSelect () {
      this.isShowSelect = false;
    },
    popoverHide () {
      if (this.multiple) {
        this.checkedIds = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据
        this.checkedData = this.$refs.tree.getCheckedNodes(); // 所有被选中的节点所组成的数组数据
      } else {
        this.checkedIds = this.$refs.tree.getCurrentKey();
        this.checkedData = this.$refs.tree.getCurrentNode();
      }
      this.$emit("popoverHide", this.checkedIds, this.checkedData, this.el);
    },
    // 节点被点击时的回调,返回被点击的节点数据
    handleNodeClick (data, node) {
      if (!this.multiple) {
        let tmpMap = {};
        tmpMap.value = node.key;
        tmpMap.label = node.label;
        this.options = [];
        this.options.push(tmpMap);
        this.selectedData = node.label;
        this.isShowSelect = !this.isShowSelect;
      }
    },
    // 节点选中状态发生变化时的回调
    handleCheckChange () {
      var checkedKeys = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据
      this.options = [];
      if (!this.rootNodeChick)
        checkedKeys.forEach(item => {
          var node = this.$refs.tree.getNode(item); // 所有被选中的节点对应的node
          let tmpMap = {};
          if (node.childNodes.length == 0) {
            tmpMap.value = node.key;
            tmpMap.label = node.label;
            this.options.push(tmpMap);
          }
        });
      else
        this.options = checkedKeys.map(item => {
          var node = this.$refs.tree.getNode(item); // 所有被选中的节点对应的node
          let tmpMap = {};
          tmpMap.value = node.key;
          tmpMap.label = node.label;
          return tmpMap;
        });
      this.selectedData = this.options.map(item => {
        return item.label;
      });
    }
  }
  
};
</script>

<style lang="scss" scoped>
  .select-popover {
    ::v-deep .el-select {
      padding-left: 5px;
    }
  }
</style>
 
<style scoped>
.mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 999;
}
.common-tree {
  /* overflow: auto; */
  overflow: hidden;
}
</style>
 
<style lang="scss">
.el-popover {
  max-height: 100vh;
  overflow: auto;
}
.tree-select .el-select__tags .el-tag .el-tag__close {
  display: none;
}
.tree-select .el-select__tags .el-tag .el-icon-close {
  display: none;
}
.el-input--suffix{
  // width: 184px !important;
}
/* .el-tree-node__expand-icon:before {
  content: '';
  border-left: 1px dashed #999999;
  height: calc(100% - 24px);
  position: absolute;
  left: 10px;
  top: 0px;
} */
.el-icon-caret-right:before{
  content: "\e723";
}
.tree-line{
  .el-tree-node {
    position: relative;
  }
  .el-tree-node::before {
    content: "";
    height: 100%;
    width: 1px;
    position: absolute;
    left: -3px;
    top: -26px;
    border-width: 1px;
    border-left: 1px dashed #E1E7E9;
  }
  .el-tree-node:last-child::before {
    height: 38px; 
  }
  .el-tree-node::after {
    content: "";
    width: 24px;
    height: 20px;
    position: absolute;
    left: -3px;
    top: 12px;
    border-width: 1px;
    border-top: 1px dashed #E1E7E9;
  }
  & > .el-tree-node::after {
    border-top: none;
  }
  & > .el-tree-node::before {
    border-left: none;
  }
  .el-tree-node__expand-icon{
    font-size: 16px;
    &.is-leaf{
      color: transparent;
      display: none;
    }
  }
}
.tree-line .el-tree-node{
  padding-left: 0px !important;
}
</style>